package com.xiaoxi.bms.common.handler;

import com.xiaoxi.bms.common.exception.BMSSecurityException;
import com.xiaoxi.bms.common.response.ApiResponse;
import com.xiaoxi.bms.common.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName BMSSecurityAuthenticationEntryPoint
 * @Description 将内容抛出给用户
 * @Author xiaoxi
 * @Date 2022/4/24 14:28
 * @Version 1.0
 */
@Slf4j
@Component
public class BMSSecurityAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
        HttpUtils.renderString(response, ApiResponse.ofSecurityException((BMSSecurityException) authException), HttpStatus.BAD_REQUEST);
    }
}
